<!-- letterAdvce -->
<template>
  <div class="page">
    <!-- 导航栏 -->
    <div class="navBar">
      <customNavBar :leftTetx="leftTetx" :titleTest="titleTest" :H5="true" @goBack="goBack"></customNavBar>
    </div>
    <!-- 页面内容 -->
    <div class="content">
      <div class="letterAdviceText">{{ t }}</div>
      <div class="textViewExperience">
        <div class='sect'>
          <div v-for="(v, b) in context" :key="b" class="textOneExperience">
            {{ v }}
          </div>
        </div>
        <div class="experienceButExp" @click="goreportingResultscz()">
          <div class="experienceButExperience" v-if="status == 0">立即体验</div>
          <div class="experienceButExperience" v-else>立即开启</div>
        </div>
        <div class="experienceButExp1">
          <div class="experienceButExperience" v-if="status == 0" @click.stop="goreportingJst()">
            暂不体验
          </div>
        </div>
      </div>
      <!-- <div class="experienceButExperience-span" v-if="status == 0">*本链接将于您打开7天后失效</div> -->
    </div>
  </div>
  <div class='pageBj' v-if="showDialog">
    <div class="pageBjDiv">
      <div class='pageBjDivHeader'>温馨提示</div>
      <div class='pageBjDivsection'>您确认要退出本次服务吗?</div>
      <div class="pageBjDivBtoom">
        <div @click="onConfirm()">继续退出</div>
        <div @click="onCancel()">取消</div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup name="addCustomers">
import { useRouter, useRoute } from "vue-router";
import { Toast } from "/@/utils/Message";
import { onMounted } from "vue";
import { reactive, ref } from "vue";
// import { JIAMIPublicData, JIMIPublicData } from '/@/utils/encrypt_sm2';
import { getUrlParam, notificationLetteradssa, validatorCustomMark } from "/@/api/auth/addCustome";
import { isTaskStatusGoView } from "/@/api/auth/task";
import { createCustomMark } from '/@/api/auth/repot'
import customNavBar from "/src/components/NavBar/customNavBar.vue";
import { sm2 } from 'sm-crypto';
import { policyListStore } from '/@/stores/policyListStore'
import { RInform } from '/@/api/reviewH5/index'
import { Dialog } from '/@/utils/Message'
import { userSubmit, subCode } from '/@/api/index'



//公钥串---加密使用
const publicKey = ref<string>("044291b381a039a8d7d02d7272d2d7c78a30d33e3eeaa0b509bf77d2376582ab2d573730e6bfe9a53bad53f53ac1a85d9c11050931f04bc8b3afc04144d5a6f9be")
//私钥串---解密使用
const privateKey = ref<string>("5aa03412c3051e1d4cf9d19cfbeeec70c28f388c9f82747cc912096c9cd44bea")
// 加密方法
const JIAMIPublicData = (data:any): string => {
  const cipherMode = 1;
  const result = sm2.doEncrypt(data, publicKey.value, cipherMode);
  return `04${result}`;
}
// 解密方法
const JIMIPublicData = (data: any): string => {
  const cipherMode = 1;
  //data为加密的数据
  const result = sm2.doDecrypt(data.substring(2), privateKey.value, cipherMode)
  return result;
}

const router = useRouter();
const route = useRoute();
const pStore = policyListStore()
const t = ref<string>('')
let context = ref<string[]>([]);
const showDialog = ref<boolean>(false)
let data = reactive({
  ...route.query,
});
// data.taskCode = '000002202312180939478946'
const leftTetx = ref<string>("");
const titleTest = ref<string>("");
var status: string | number = ''
let jstUrl = ''
const num = ref('0')
const goreportingResultscz = (): void => {
  if (status == '0') {
    // 告知信
    router.push({ path: '/authorization', query: { statusBar: 'true', navigator: 'true', taskNo: JIMIPublicData((data.taskCode as string)) } })
    // window.location.href = `${jstUrl}?statusBar=true&navigator=true&taskNo=${data.taskCode}`
  } else {
    if (data.taskCode) {
      // 温馨提示
      validatorCustomMark({ taskCode: data.taskCode }).then((res: any) => {
        if (res.code == 200) {
          if (res.status == '0') {
            // 等于O的时候 taskCode就是customToken
            sessionStorage.setItem('customToken', res.taskCode)
            //  router.push({ path: 'detailedVersion', query: { taskCode: data.taskCode, isYsc: "1", jstUrl: jstUrl } })
            if (num.value == '6' || num.value == '15') {
              router.push({ path: 'reportingResultxysc', query: { taskCode: data.taskCode, isYsc: "1" } })
            } else if (num.value == '18') {
              router.push({ path: 'reportingResultybc', query: { taskCode: data.taskCode, isYsc: "1" } })
            }
          }
        } else if (res.code == 402) {
          //  window.location.href = `${jstUrl}?statusBar=true&navigator=true&taskNo=${data.taskCode}`
          router.push({ path: '/authorization', query: { statusBar: 'true', navigator: 'true', taskNo: JIMIPublicData((data.taskCode as string)), letter: num.value } })
        }
      })
    }
  }
}
const goBack = () => {
  goreportingJst()
}
const onConfirm = () => {
  // window.location.replace('./reportingResultwxts')
  router.push({ path: 'reportingResultwxts' })
}
const onCancel = () => {
  // 取消操作
  showDialog.value = false;
}
const goreportingJst = (): void => {
  showDialog.value = true
}
// 解密报文
const getHtml = (t: any) => {
  //   t = "尊敬的44b2d43eb5aa0441ac8dba86f8753515，大哥好！|太平洋保险保险公司代理人管理员邀请您使用保检视服务(以下简称本服务)本服务是指运用保险行业数据，对您的保险保障情况进行整合分析，整理并形成涵盖保单总览、保障透视、费用筹划、客户权益等内容的保单检视报告。| 本服务由中国银行保险信息技术管理有限公司(简称中国银保信)提供。中国银保信主要负责建设和运营全国统一的银行业、保险业信息共享系统，提供交易支持、数据治理、风险管理、科技支撑和登记、验证精算、咨询等服务。|";
  let array = (t as string).split("|");
  context.value = array.slice(0, -1);
};
// 发送验证码
const formList = reactive({
    mobile: '',
    smsCode: ''
})
const codeTextflag = ref('获取验证码')
const isSendCode = ref(false)
let timer:any = ''
const sendCode = () => {
    if (codeTextflag.value.indexOf('验证码') <= 0) return
    let newobj = { mobile: formList.mobile as string }
    let params: any = JSON.stringify(newobj)
        userSubmit(params).then((res: any) => {
            // sendSms(store.transactionId as any).then((res:any) => {
            if (res.code == 200) {
                if (res.data.result) {
                    Toast('验证码已发出')
                    isSendCode.value = true
                    let n = 60
                    timer = setInterval(() => {
                        codeTextflag.value = `${n}s`
                        n--
                        if (n < 0) {
                            clearInterval(timer)
                            codeTextflag.value = '获取验证码'
                        }
                    }, 1000)
                } else {
                    Toast('获取验证码错误,请重新获取')
                }
            } else {
                codeTextflag.value = '获取验证码'
                Dialog({
                    message: res.msg || '操作错误', // '您的手机号已被占用注册，请更换手机号。'
                })
            }
        })
}
// 效验验证码
const onSubmit = () => {
  if (!formList.smsCode) {
        Toast('请获取验证码')
        return
    }
  let newobj = {
    credential: formList.smsCode as any,
    mobile: formList.mobile as string,
  }
  let params: any = JSON.stringify(newobj)
  subCode(params).then((res: any) => {
    if(res.code == 200) {
      if(res.data.result){
        callbackSuccess()
      } else {
        Toast('验证码错误,请输入正确的验证码')
      }
    } else {
      Toast('操作错误')
    }
  })
}
//创建唯一标识
const callbackSuccess = async () => {
     const a =  await createCustomMark({ taskCode: data.taskCode})
    if (a.code == 200) {
        sessionStorage.removeItem('customToken')
        sessionStorage.setItem('customToken', a.customToken)
    if (num.value == '6' || num.value == '15') {
      router.push({ path: 'reportingResultxysc', query: { taskCode: data.taskCode, isYsc: "1" } })
    } else if (num.value == '18') {
      router.push({ path: 'reportingResultybc', query: { taskCode: data.taskCode, isYsc: "1" } })
    }
    }
}
onMounted(() => {
  setTimeout(()=>{
    const taskNo:any =  data.taskCode 
    RInform(taskNo).then((res: any) => {
      // console.log(res)
    })
  },1700)
  //从告知页进来的首先清一下customToken
  //如果告知信地址栏有sl，存起来，方便后退
  if (data.sl) {
    pStore.setLetterAdvceSL(data.sl)
  }
  //  从生成失败来一键重新检视
  if (data.isScsb) {
    noti(data)
  }
  if (data.taskCode && !data.isScsb) {
    //有taskCode是扫码长链
    //根据taskcode请求一飞的接口，返回taskstatus
    //根据taskstatus判断跳转哪个页面
    // 根据任务状态控制用户访问页面
    isTaskStatusGoView({ taskCode: (data as any).taskCode }).then((res: any) => {
      switch (res.taskStatus) {
        case '1': // 已创建
          noti(data)
          break;
        case '2': // 已查看告知页
          noti(data)
          break;
        case '3': // 已授权认证
          noti(data)
          break;
        case '4': // 已确认保单范围
          noti(data)
          break;
        case '5': // 生成中
          router.push({ path: '/reportingResultscz', query: { taskCode: res.taskCode, reportCode: res.reportCode } })
          break;
        case '6':  // 已生成 
          noti(data)
          num.value = res.taskStatus
          break;
        case '10':  // 无有效保单
          router.push({ path: '/reportingResultswyx', query: { taskCode: res.taskCode, reportCode: res.reportCode } })
          break;
        case '11': // 无可检视
          router.push({ path: '/reportingResultswkjs', query: { taskCode: res.taskCode, reportCode: res.reportCode } })
          break;
        case '12': // 生成失败
          router.push({ path: '/reportingResultcsb', query: { taskCode: res.taskCode, reportCode: res.reportCode, errCode: res.errCode } })
          break;
        case '14': // 报告结果不一致
          router.push({ path: '/reportingResultsbyz', query: { taskCode: res.taskCode, reportCode: res.reportCode } })
          break;
        case '15': // 部分成功
          noti(data)
          num.value = res.taskStatus
          // router.push({ path: '/reportingResultlecs', query: { taskCode: res.taskCode, reportCode: res.reportCode } })
          break;
        case '16':
          router.push({ path: '/reportingResultcsb', query: { taskCode: res.taskCode, reportCode: res.reportCode, errCode: res.errCode } })
          break;
        case '17': // 报告超时
          router.push({ path: '/reportingResultlecs', query: { taskCode: res.taskCode, reportCode: res.reportCode } })
          break;
        case '18': // 已保存
          noti(data)
          num.value = res.taskStatus
          // router.push({ path: '/reportingResultybc', query: { taskCode: res.taskCode, reportCode: res.reportCode } })
          break;
      }
    })
  } else if (!data.isScsb) {   //一键重新检视生成不走这里
    //复制短链接
    getUrlParam(data).then((res: any) => {
      if (res.code == 200) {
        //根据taskcode请求一飞的接口，返回taskstatus
        //根据taskstatus判断跳转哪个页面
        // 根据任务状态控制用户访问页面
        isTaskStatusGoView({ taskCode: res.param }).then((v: any) => {
          switch (v.taskStatus) {
            case '1':
              noti(res)
              break;
            case '2':
              noti(res)
              break;
            case '3':
              noti(res)
              break;
            case '4':
              noti(res)
              break;
            case '5': // 生成中
              router.push({ path: '/reportingResultscz', query: { taskCode: res.param } })
              break;
            case '6':  // 已生成
              noti(res)
              num.value = v.taskStatus
              break;
            case '10':  // 无有效保单
              router.push({ path: '/reportingResultswyx', query: { taskCode: res.param } })
              break;
            case '11': // 无可检视
              router.push({ path: '/reportingResultswkjs', query: { taskCode: res.param } })
              break;
            case '12': // 生成失败
              router.push({ path: '/reportingResultcsb', query: { taskCode: res.param, errCode: v.errCode } })
              break;
            case '14': // 报告结果不一致
              router.push({ path: '/reportingResultsbyz', query: { taskCode: res.param } })
              break;
            case '15': // 部分成功
              noti(res)
              num.value = v.taskStatus
              // router.push({ path: '/reportingResultlecs', query: { taskCode: res.taskCode, reportCode: res.reportCode } })
              break;
            case '16':
              router.push({ path: '/reportingResultcsb', query: { taskCode: v.taskCode, reportCode: v.reportCode, errCode: v.errCode } })
              break;
            case '17': // 报告超时
              router.push({ path: '/reportingResultlecs', query: { taskCode: v.taskCode, reportCode: v.reportCode } })
              break;
            case '18': // 已保存
              noti(res)
              num.value = v.taskStatus
              // router.push({ path: '/reportingResultybc', query: { taskCode: v.taskCode, reportCode: v.reportCode } })
              break;
          }
        })
      } else {
        router.push({ path: '/reportingResultlecs' })
      }
    })
  }


});
let noti = (x: any) => {
  notificationLetteradssa({ taskCode: x.param || x.taskCode }).then((v: any) => {
    if (v.code == 200) {
      data.taskCode = x.param ? x.param : x.taskCode
      status = v.status
      jstUrl = v.jstUrl
      getHtml(v.context)
      if (v.status == 0) {
        t.value = '邀请信'
      } else {
        t.value = '温馨提示'
      }
    }
  })
}
</script>

<!-- less嵌套设置 -->
<style lang="less" scoped>
.photo {
  margin-top: 100px;
  .photoDv {
    display: flex;
    justify-content: space-between;
    input {
      border: 1px solid;
    }
    .photoDvCode {
      display: flex;
      justify-content: space-between;
    }
  }
}
.pageBj {
  top: 0;
  width: 100%;
  height: 100%;
  display: flex;
  position: absolute;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.7);

  .pageBjDiv {
    width: 300px;
    // padding: 10px 20px;
    border-radius: 10px;
    background: #ffffff;

    .pageBjDivHeader {
      margin-top: 17px;
      font-size: 14px;
      font-weight: bold;
    }

    .pageBjDivsection {
      margin-top: 17px;
      font-size: 13px;
    }

    .pageBjDivBtoom {
      display: flex;
      width: 100%;
      margin-top: 17px;
      justify-content: center;
      border-top: 1px solid #f3f3f3;

      div {
        padding: 10px 0;
        width: 50%;
      }

      div:last-child {
        color: #ffffff;
        background: #2c56ce;
        border-bottom-right-radius: 7px;
      }
    }
  }
}

.page {
  background-color: #deecf6;

  .content {
    width: 100%;
    height: 100vh;
    overflow: hidden;
    padding-top: 68px;
    box-sizing: border-box;
    background-color: #deecf6;

    .letterAdviceText {
      margin-top: 6px;
      font-size: 24px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 250;
      color: #143766;
      letter-spacing: 1px;
    }

    .textViewExperience {
      padding: 0px;
      width: 100vw;
      height: 90%;
      margin-top: 8px;
      background: url("/src/assets/img/features/letterAdviceBg.png");
      background-size: 100% 100%;
      text-align: left;
      overflow: hidden;

      .sect {
        width: 270px;
        height: 55%;
        margin-top: 70px;
        margin-left: 59px;
        overflow-y: auto;

        // overflow-x: scroll;
        .textOneExperience:first-child {
          // margin-top: 70px !important;
        }

        .textOneExperience {
          width: 97%;
          font-size: 14px;
          margin-top: 10px;
          color: #222222;
          font-weight: 300;
        }
      }

      .experienceButExp {
        width: 100%;
        bottom: 107px;
        display: flex;
        position: fixed;
        align-items: center;
        justify-content: center;

        .experienceButExperience {
          background: url("/src/assets/img/features/experienceBut.png");
          background-size: 100% 100%;
          width: 190px;
          height: 50px;
          font-size: 19px;
          color: #ffffff;
          line-height: 47px;
          text-align: center;
        }
      }

      .experienceButExp1 {
        width: 100%;
        bottom: 70px;
        display: flex;
        position: fixed;
        align-items: center;
        justify-content: center;

        .experienceButExperience {
          // background: url("/src/assets/img/features/experienceBut.png");
          // background-size: 100% 100%;
          width: 190px;
          height: 50px;
          font-size: 17px;
          color: #ffffff;
          line-height: 47px;
          text-align: center;
          // opacity: .5;
        }
      }
    }

    .experienceButExperience-span {
      position: absolute;
      color: #222222;
      line-height: 1px;
      font-size: 13px;
      // text-align: left;
      transform: translate(20%, -15px);
    }
  }
}
</style>


<!-- 
    1.有从业人员信息的---告知信
    客户姓名	仅展示	文本
    保险机构	仅展示	文本
    从业人员姓名	仅展示	文本
    其他文案		
    立即体验		按钮

    2.无从业人员信息---告知信
    客户姓名	仅展示	文本
    保险机构	仅展示	文本
    立即体验		按钮

    3.告知信（查看报告）
    报告名称	仅展示	文本			
    报告生成时间	仅展示	文本			
    保险机构	仅展示	文本			
    代理人	仅展示	文本			即从业人员姓名
    立即开启		按钮			


 -->
